package samples.java.algorithm.sort;

/**
 * @author wang.yuchuan
 * @createdAt 2018/12/9
 */
public class InsertionSort {

    public static void sort(int[] arr) {
        int preIndex, current;

        for (int i = 1; i < arr.length; i++) {
            preIndex = i - 1;
            current = arr[i];
            while (preIndex >= 0 && arr[preIndex] > current) {
                arr[preIndex + 1] = arr[preIndex];
                preIndex--;
            }
            arr[preIndex + 1] = current;
        }

    }

    public static void main(String[] args) {
        int[] arr = {3, 44, 5, 67, 4, 23, 42, 56, 78, 91, 2, 7, 9, 12, 16};

        sort(arr);

        for (int i : arr) {
            System.out.print(i + " ");
        }
    }
}
